{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "e54befd2-7b67-4681-899f-3c4a8462d178",
   "metadata": {},
   "source": [
    "【1】"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1f2b03ce-ff56-4a01-bdf3-576244e3f944",
   "metadata": {},
   "source": [
    "单数据系列柱形图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "26cc82ff-6612-48f9-b2ef-2ec8477e0edd",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\abc18\\AppData\\Local\\Temp\\ipykernel_15816\\1470194722.py:6: FutureWarning: The argument 'date_parser' is deprecated and will be removed in a future version. Please use 'date_format' instead, or read your data in as 'object' dtype and then call 'to_datetime'.\n",
      "  data = pd.read_excel('./data/subject.xlsx',date_parser='Month')\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Name</th>\n",
       "      <th>Month</th>\n",
       "      <th>Average</th>\n",
       "      <th>Gain</th>\n",
       "      <th>Gain_p</th>\n",
       "      <th>Peak</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>AI-NLP</td>\n",
       "      <td>2022-08-01</td>\n",
       "      <td>638072</td>\n",
       "      <td>45094</td>\n",
       "      <td>0.0760</td>\n",
       "      <td>1039161</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>AI-NLP</td>\n",
       "      <td>2022-07-01</td>\n",
       "      <td>592978</td>\n",
       "      <td>23436</td>\n",
       "      <td>0.0411</td>\n",
       "      <td>927570</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>AI-NLP</td>\n",
       "      <td>2022-06-01</td>\n",
       "      <td>569542</td>\n",
       "      <td>8263</td>\n",
       "      <td>0.0147</td>\n",
       "      <td>906968</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>AI-NLP</td>\n",
       "      <td>2022-05-01</td>\n",
       "      <td>561279</td>\n",
       "      <td>13026</td>\n",
       "      <td>0.0238</td>\n",
       "      <td>932615</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>AI-NLP</td>\n",
       "      <td>2022-04-01</td>\n",
       "      <td>548253</td>\n",
       "      <td>-31282</td>\n",
       "      <td>-0.0540</td>\n",
       "      <td>1016762</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>302</th>\n",
       "      <td>AI-Safe</td>\n",
       "      <td>2017-08-01</td>\n",
       "      <td>389527</td>\n",
       "      <td>159825</td>\n",
       "      <td>0.6958</td>\n",
       "      <td>874171</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>303</th>\n",
       "      <td>AI-Safe</td>\n",
       "      <td>2017-07-01</td>\n",
       "      <td>229702</td>\n",
       "      <td>89800</td>\n",
       "      <td>0.6419</td>\n",
       "      <td>481291</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>304</th>\n",
       "      <td>AI-Safe</td>\n",
       "      <td>2017-06-01</td>\n",
       "      <td>139902</td>\n",
       "      <td>32460</td>\n",
       "      <td>0.3021</td>\n",
       "      <td>267194</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>305</th>\n",
       "      <td>AI-Safe</td>\n",
       "      <td>2017-05-01</td>\n",
       "      <td>107442</td>\n",
       "      <td>37647</td>\n",
       "      <td>0.5394</td>\n",
       "      <td>189456</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>306</th>\n",
       "      <td>AI-Safe</td>\n",
       "      <td>2017-04-01</td>\n",
       "      <td>69795</td>\n",
       "      <td>52244</td>\n",
       "      <td>2.9767</td>\n",
       "      <td>140104</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>307 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        Name      Month  Average    Gain  Gain_p     Peak\n",
       "0     AI-NLP 2022-08-01   638072   45094  0.0760  1039161\n",
       "1     AI-NLP 2022-07-01   592978   23436  0.0411   927570\n",
       "2     AI-NLP 2022-06-01   569542    8263  0.0147   906968\n",
       "3     AI-NLP 2022-05-01   561279   13026  0.0238   932615\n",
       "4     AI-NLP 2022-04-01   548253  -31282 -0.0540  1016762\n",
       "..       ...        ...      ...     ...     ...      ...\n",
       "302  AI-Safe 2017-08-01   389527  159825  0.6958   874171\n",
       "303  AI-Safe 2017-07-01   229702   89800  0.6419   481291\n",
       "304  AI-Safe 2017-06-01   139902   32460  0.3021   267194\n",
       "305  AI-Safe 2017-05-01   107442   37647  0.5394   189456\n",
       "306  AI-Safe 2017-04-01    69795   52244  2.9767   140104\n",
       "\n",
       "[307 rows x 6 columns]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from pyecharts.charts import Bar # 绘制条形图的模块\n",
    "from pyecharts import options as opts  # 配置项模块\n",
    "from pyecharts.faker import Faker # 用于生成假数据\n",
    "from pyecharts.globals import ThemeType # 主题配置项\n",
    "import pandas as pd\n",
    "data = pd.read_excel('./data/subject.xlsx',date_parser='Month')\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "f032e8bf-c7df-434a-a9d3-6ac34afd01d7",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\abc18\\AppData\\Local\\Temp\\ipykernel_30620\\3242580082.py:6: FutureWarning: The argument 'date_parser' is deprecated and will be removed in a future version. Please use 'date_format' instead, or read your data in as 'object' dtype and then call 'to_datetime'.\n",
      "  data = pd.read_excel('./data/subject.xlsx',date_parser='Month')\n",
      "C:\\Users\\abc18\\AppData\\Local\\Temp\\ipykernel_30620\\3242580082.py:8: FutureWarning: 'Y' is deprecated and will be removed in a future version, please use 'YE' instead.\n",
      "  data_y = data_AIeye.set_index('Month').resample('Y').sum()\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Gain</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Month</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2012-12-31</th>\n",
       "      <td>69203</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-12-31</th>\n",
       "      <td>244682</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2014-12-31</th>\n",
       "      <td>157334</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-12-31</th>\n",
       "      <td>49404</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-12-31</th>\n",
       "      <td>20577</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-12-31</th>\n",
       "      <td>-80847</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-12-31</th>\n",
       "      <td>-73706</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-12-31</th>\n",
       "      <td>-53068</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-12-31</th>\n",
       "      <td>36144</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-12-31</th>\n",
       "      <td>24213</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-12-31</th>\n",
       "      <td>20062</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              Gain\n",
       "Month             \n",
       "2012-12-31   69203\n",
       "2013-12-31  244682\n",
       "2014-12-31  157334\n",
       "2015-12-31   49404\n",
       "2016-12-31   20577\n",
       "2017-12-31  -80847\n",
       "2018-12-31  -73706\n",
       "2019-12-31  -53068\n",
       "2020-12-31   36144\n",
       "2021-12-31   24213\n",
       "2022-12-31   20062"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from pyecharts.charts import Bar # 绘制条形图的模块\n",
    "from pyecharts import options as opts  # 配置项模块\n",
    "from pyecharts.faker import Faker # 用于生成假数据\n",
    "from pyecharts.globals import ThemeType # 主题配置项\n",
    "import pandas as pd\n",
    "data = pd.read_excel('./data/subject.xlsx',date_parser='Month')\n",
    "data_AIeye = data[data['Name']=='AI-视觉'][['Month','Gain']]  # 按年对数据进行重采用\n",
    "'''\n",
    "data[data['Name']=='AI-视觉']: 这部分代码从data DataFrame中筛选出Name列值为AI-视觉的行。\n",
    "[['Month','Gain']]: 然后，从这些筛选后的行中选择Month和Gain两列，创建一个新的DataFrame data_AIeye\n",
    "'''\n",
    "data_y = data_AIeye.set_index('Month').resample('Y').sum()\n",
    "'''\n",
    "data_AIeye.set_index('Month'): 这部分代码将data_AIeye DataFrame的Month列设置为索引。\n",
    "这样做是为了能够对日期进行时间序列操作。\n",
    ".resample('Y'): resample函数用于对时间序列数据进行重采样。这里，'Y'表示按年重采样。\n",
    "这意味着数据将被聚合到每年的基础上。\n",
    ".sum(): 最后，sum()函数对重采样后的数据进行聚合，计算每年Gain列的总和。\n",
    "'''\n",
    "data_y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "1c0a7438-ac76-4f28-8bc9-6385c50843b3",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\abc18\\AppData\\Local\\Temp\\ipykernel_15816\\1544504124.py:6: FutureWarning: The argument 'date_parser' is deprecated and will be removed in a future version. Please use 'date_format' instead, or read your data in as 'object' dtype and then call 'to_datetime'.\n",
      "  data = pd.read_excel('./data/subject.xlsx',date_parser='Month')\n",
      "C:\\Users\\abc18\\AppData\\Local\\Temp\\ipykernel_15816\\1544504124.py:8: FutureWarning: 'Y' is deprecated and will be removed in a future version, please use 'YE' instead.\n",
      "  data_y = data_AIeye.set_index('Month').resample('Y').sum().astype(int)['Gain'].tolist()\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'D:\\\\jupyter\\\\kagge竞赛非职业人员统计图.html'"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from pyecharts.charts import Bar # 绘制条形图的模块\n",
    "from pyecharts import options as opts  # 配置项模块\n",
    "from pyecharts.faker import Faker # 用于生成假数据\n",
    "from pyecharts.globals import ThemeType # 主题配置项\n",
    "import pandas as pd\n",
    "data = pd.read_excel('./data/subject.xlsx',date_parser='Month')\n",
    "data_AIeye = data[data['Name']=='AI-视觉'][['Month','Gain']]  # 按年对数据进行重采用\n",
    "data_y = data_AIeye.set_index('Month').resample('Y').sum().astype(int)['Gain'].tolist()\n",
    "# 计算之后，转为int后将其转化为列表\n",
    "data_x = data_AIeye.loc[:,'Month'].dt.year.unique().tolist()  # 将年份单独取出转换为list\n",
    "\n",
    "bar = (Bar(init_opts=opts.InitOpts(theme=ThemeType.DARK,bg_color='#080b30'))\n",
    "      .add_xaxis(data_x)  # 添加x轴信息，传入列表，元组序列格式\n",
    "      .add_yaxis('年增长人数',data_y) # 第一个参数系列名称，第二个参数是要显示的数据\n",
    "      .set_global_opts(title_opts=opts.TitleOpts(\n",
    "          title='AI-视觉 kaggel国家竞赛 非职业人员 年增长人数柱形图',\n",
    "          pos_left='center'),# 标题配置项，设置标题以及标题的位置，此处表示标题居中，默认是居左\n",
    "        legend_opts=opts.LegendOpts(pos_top='5%'), # 图例设置，图例的位置\n",
    "        tooltip_opts=opts.TooltipOpts(trigger='axis',\n",
    "                                     axis_pointer_type='shadow')) # 提示框组件配置，显示样式为阴影\n",
    ")\n",
    "bar.render(\"kagge竞赛非职业人员统计图.html\") # 渲染图表到网页"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "09d00a7b-03d1-43a9-a7fa-82cad0754654",
   "metadata": {},
   "source": [
    "【2】"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a71c5e97-592c-4abf-8774-49ef2dc7cb55",
   "metadata": {},
   "source": [
    "可以使用itemstyle_opts属性来修改柱形图样式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "8543d375-287d-4bc8-9312-1593fc1874a1",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\abc18\\AppData\\Local\\Temp\\ipykernel_20040\\2281594175.py:6: FutureWarning: The argument 'date_parser' is deprecated and will be removed in a future version. Please use 'date_format' instead, or read your data in as 'object' dtype and then call 'to_datetime'.\n",
      "  data = pd.read_excel('./data/subject.xlsx',date_parser='Month')\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'D:\\\\jupyter\\\\根据itemstyle_opts修改柱形图属性.html'"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from pyecharts.charts import Bar # 绘制条形图的模块\n",
    "from pyecharts import options as opts  # 配置项模块\n",
    "from pyecharts.faker import Faker # 用于生成假数据\n",
    "from pyecharts.globals import ThemeType # 主题配置项\n",
    "import pandas as pd\n",
    "data = pd.read_excel('./data/subject.xlsx',date_parser='Month')\n",
    "data_AISafe = data[data['Name']=='AI-Safe']\n",
    "# 取出时间后进行格式化操作样本中的时间 例如2022/8/1 变为xxxx年-x月\n",
    "data_x = data_AISafe['Month'].apply(lambda x:x.strftime('%Y-%m')).tolist()[::-1]\n",
    "# 取出Gain这列 作为y轴\n",
    "data_y = data_AISafe['Gain'].tolist()[::-1]\n",
    "\n",
    "bar = (Bar(init_opts=opts.InitOpts(theme=ThemeType.DARK,bg_color='#080b30'))\n",
    "      .add_xaxis(data_x)\n",
    "      .add_yaxis('增长人数',\n",
    "                data_y,\n",
    "                itemstyle_opts={\n",
    "                    \"normal\":{\n",
    "                        \"color\":'rgba(255,105,180,0.9)',  # 前面是rgb颜色，最后一个是透明度\n",
    "                        \"barBorderRadius\":[2,2,2,2],\n",
    "                        \"shadowColor\":'rgba(108,80,243,0.9)',\n",
    "                        \"shadowBlur\":20\n",
    "                    }\n",
    "                }) # 修改柱子样式\n",
    "       .set_global_opts(\n",
    "           title_opts=opts.TitleOpts(title='AI safe kagge竞赛非职业人员 月增长人数柱形图',\n",
    "                                    pos_left='center'),\n",
    "           legend_opts=opts.LegendOpts(pos_top='5%'),  # 图例设置，图例的位置\n",
    "           tooltip_opts=opts.TooltipOpts(trigger='axis',axis_pointer_type='shadow'))\n",
    "       .set_series_opts(label_opts=opts.LabelOpts(is_show=False))\n",
    "      )\n",
    "bar.render('根据itemstyle_opts修改柱形图属性.html')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "de352e30-8923-417d-b63a-0ccc55bc7339",
   "metadata": {},
   "source": [
    "【3】"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eee66542-d7b4-4301-a295-3edd527686bc",
   "metadata": {},
   "source": [
    "单数据系列不同的颜色样式"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c72b5655-fb60-4ea9-8725-338cb3e0314a",
   "metadata": {},
   "source": [
    "在柱形图中，可以使用jscode这样的javascript封装后的代码进行操作，同时可以使用itemstyle_opts样式配置信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "8a23e21b-1c1a-4a98-8a77-4f25cd4c4133",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'D:\\\\jupyter\\\\AISafe_kaggle在线人数每月变化.html'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from pyecharts.charts import Bar\n",
    "from pyecharts import options as opts\n",
    "from pyecharts.globals import ThemeType,JsCode  # 主题配置和jscode\n",
    "import pandas as pd\n",
    "\n",
    "data = pd.read_excel('./data/subject.xlsx',parse_dates=['Month'])\n",
    "# 筛选出ALsafe的数据\n",
    "data_AISafe = data[data['Name'] == 'AI-Safe']\n",
    "\n",
    "# 处理x轴数据\n",
    "data_x = data_AISafe['Month'].apply(lambda x:x.strftime('%Y-%m')).tolist()[::-1] \n",
    "# 通过格式化时间变成2024-10类似格式，而后进行切割，这样格式的数据，最后将处理完成的数据放入到list中\n",
    "# 处理y轴数据\n",
    "data_y = data_AISafe['Gain'].tolist()[::-1]\n",
    "\n",
    "# 定义样式\n",
    "'''\n",
    "JsCode用于插入JavaScript代码来创建线性渐变效果.\n",
    "这是pyecharts中一种强大的功能，允许用户自定义图表的外观。\n",
    "style_1和style_2是两个字典，分别定义了两种不同渐变颜色的样式。\n",
    "它们使用JsCode来插入JavaScript代码，这段代码创建了一个线性渐变对象。\n",
    "style_1是从绿色到透明绿色的渐变，style_2是从粉红色到透明粉红色的渐变。\n",
    "'''\n",
    "style_1 = {\n",
    "    \"normal\":{\n",
    "        \"color\":JsCode(\"\"\"new echarts.graphic.LinearGradient(0,0,0,1,[{\n",
    "        offset:0,color:'rgba(0,202,149,0.9)'},\n",
    "        {offset:1,color:'rgba(0,202,149,0)'}],false)\"\"\"),\n",
    "        \"shadowColor\":'rgba(0,202,149,0.9)',\n",
    "        \"shadowBlur\":20\n",
    "    }\n",
    "}\n",
    "\n",
    "style_2 = {\n",
    "    \"normal\":{\n",
    "        \"color\":JsCode(\"\"\"new echarts.graphic.LinearGradient(0,0,0,1,[{\n",
    "        offset:1,color:'rgba(255,105,180,0.9)'},\n",
    "        {offset:0,color:'rgba(255,105,180,0)'}],false)\"\"\"),\n",
    "        \"shadowColor\":'rgba(108,80,243,0.9)',\n",
    "        \"shadowBlur\":20\n",
    "    }\n",
    "}\n",
    "\n",
    "# 根据Grain值创建格式化Y轴数据\n",
    "formatted_data_y = []\n",
    "for x,y in zip(data_x,data_y):\n",
    "    y = float(y)  # 放置类型转换异常，将y中的数据转换为float类型后，交给y保存\n",
    "    if y > 0:  # 如果当前y中是有数据的\n",
    "        # 通过baritem中的属性itemstyle_opts将上面设定的JScode样式设置到柱形图中\n",
    "        formatted_data_y.append(opts.BarItem(name=x,value=y,itemstyle_opts=style_1))\n",
    "    else:\n",
    "        formatted_data_y.append(opts.BarItem(name=x,value=y,itemstyle_opts=style_2))\n",
    "\n",
    "# 创建柱形图\n",
    "bar = (\n",
    "    Bar(init_opts=opts.InitOpts(bg_color='#080b30',theme='dark'))\n",
    "    .add_xaxis(xaxis_data=data_x)\n",
    "    .add_yaxis(\"在线人数变化\",y_axis=formatted_data_y,label_opts=opts.LabelOpts(is_show=False))\n",
    "    .set_global_opts(\n",
    "        legend_opts=opts.LegendOpts(is_show=True),\n",
    "        title_opts=opts.TitleOpts(title='AISafe kaggle在线人数每月变化趋势',pos_left='center')\n",
    "    )\n",
    ")\n",
    "\n",
    "bar.render(\"AISafe_kaggle在线人数每月变化.html\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a684543a-2051-435c-807b-e64dc0c5511e",
   "metadata": {},
   "source": [
    "在柱形图中，可以使用jscode这样的javascript封装后的代码进行操作，同时可以使用itemstyle_opts样式配置信息"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2960777e-8475-47e8-9ab6-7fa0c2ec532e",
   "metadata": {},
   "source": [
    "【4】"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "26086ca7-e5d3-4b34-afd1-11b3b0893c79",
   "metadata": {},
   "source": [
    "多数据系列柱形图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "07d78d1b-fdf6-40c5-9f7e-88c0deb7f928",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'D:\\\\jupyter\\\\历年奥运会中国奖牌数量统计图.html'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "from pyecharts.charts import Bar\n",
    "from pyecharts import options as opts\n",
    "from pyecharts.globals import ThemeType\n",
    "data = pd.read_excel('./data/奖牌.xlsx')\n",
    "# 提取近五届奥运会数据\n",
    "dt1 = data.loc[:,['赛事','金牌']].values.tolist()[-5::] # 提取近五届奥运会的赛事名称和金牌数量\n",
    "dt2 = data.loc[:,['赛事','银牌']].values.tolist()[-5::] # 提取近五届奥运会的赛事名称和金牌数量\n",
    "dt3 = data.loc[:,['赛事','铜牌']].values.tolist()[-5::] # 提取近五届奥运会的赛事名称和金牌数量\n",
    "\n",
    "# 定义基础样式\n",
    "base_itemstyle = {\n",
    "    \"normal\":{  # 正常状态下的模式\n",
    "        \"shadowBlur\":4,  # 阴影模糊程度的范围大小\n",
    "        \"shadowOffsetY\":4,  # 阴影在y轴上的偏移量\n",
    "        \"shadowOffsetX\":4,  # 阴影在x轴上的偏移量\n",
    "        \"barBorderBadius\":[2,2,2,2],  # 柱状图边角的圆角半径，即圆润程度\n",
    "        \"opacity\":1  # 不透明度\n",
    "    }\n",
    "}\n",
    "\n",
    "# 为每个系列创建不同的样式\n",
    "itemstyle1 = base_itemstyle.copy()  # 复制基础样式\n",
    "itemstyle1['normal']['shadowColor'] = 'rgba(78,112,240,.5)'  # 设置金牌的阴影颜色\n",
    "\n",
    "itemstyle2 = base_itemstyle.copy()  # 复制基础样式\n",
    "itemstyle2['normal']['shadowColor'] = 'rgba(0,197,210,.5)' # 设置银牌的阴影颜色\n",
    "\n",
    "itemstyle3 = base_itemstyle.copy()  # 复制基础样式\n",
    "itemstyle3['normal']['shadowColor'] = 'rgba(255,206,43,.5)' # 设置铜牌的阴影颜色\n",
    "\n",
    "c = (\n",
    "    Bar(init_opts=opts.InitOpts(theme=ThemeType.LIGHT,bg_color='#ffffff')) # 初始化柱形图，设置主题和背景颜色\n",
    "    .add_xaxis([i[0] for i in dt1])  # 添加x轴数据，即赛事名称\n",
    "    .add_yaxis(\"金牌\",[i[1] for i in dt1],category_gap='30%',itemstyle_opts=itemstyle1) # 添加金牌系列，设置类别间距和样式\n",
    "    .add_yaxis(\"银牌\",[i[1] for i in dt2],category_gap='30%',itemstyle_opts=itemstyle2)\n",
    "    .add_yaxis(\"铜牌\",[i[1] for i in dt3],category_gap='30%',itemstyle_opts=itemstyle3)\n",
    "    .set_series_opts(label_opts=opts.LabelOpts(position='top')) # 设置系列标签的位置\n",
    "    .set_global_opts(\n",
    "        title_opts=opts.TitleOpts(title='历年奥运会中国奖牌数量统计柱形图',pos_left='center',pos_top='2%'), # 设置图表标题及其位置\n",
    "        legend_opts=opts.LegendOpts(pos_top='7%'), # 设置图例的位置\n",
    "        xaxis_opts=opts.AxisOpts(name='赛事名称'), # 设置x轴的名称\n",
    "        toolbox_opts=opts.TooltipOpts(trigger='axis',axis_pointer_type='shadow'), # 设置提示框的触发方式和指针类型\n",
    "        # 设置y轴分割线的显示和样式\n",
    "        yaxis_opts=opts.AxisOpts(splitline_opts=opts.SplitLineOpts(is_show=True,linestyle_opts=opts.LineStyleOpts(type_='dotted')))\n",
    "    )\n",
    "    .set_colors(colors=['#4e70f0','#00c5d2','#ffce2b']) # 设置图表颜色\n",
    ")\n",
    "c.render('历年奥运会中国奖牌数量统计图.html')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "5b6fe9f2-636e-473b-9e1d-9c03707bb1f4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'D:\\\\jupyter\\\\multi_series_bar_chart.html'"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from pyecharts.charts import Bar\n",
    "from pyecharts import options as opts\n",
    "\n",
    "# 创建一个 Bar 实例\n",
    "bar = Bar()\n",
    "\n",
    "# 添加 X 轴数据\n",
    "categories = [\"苹果\", \"香蕉\", \"橙子\", \"葡萄\", \"草莓\"]\n",
    "bar.add_xaxis(categories)\n",
    "\n",
    "# 添加多个数据系列\n",
    "bar.add_yaxis(\"商家A\", [5, 20, 36, 10, 75])\n",
    "bar.add_yaxis(\"商家B\", [15, 6, 45, 20, 35])\n",
    "bar.add_yaxis(\"商家C\", [25, 16, 5, 10, 65])\n",
    "\n",
    "# 设置全局配置项\n",
    "bar.set_global_opts(\n",
    "    title_opts=opts.TitleOpts(title=\"多数据系列柱形图\"),\n",
    "    xaxis_opts=opts.AxisOpts(name=\"水果\"),\n",
    "    yaxis_opts=opts.AxisOpts(name=\"销量\"),\n",
    "    legend_opts=opts.LegendOpts(is_show=True)\n",
    ")\n",
    "\n",
    "# 渲染图表到本地 HTML 文件\n",
    "bar.render(\"multi_series_bar_chart.html\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8cc18f00-b530-4846-aa84-aaa9298610b9",
   "metadata": {},
   "source": [
    "【5】"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f3527bcc-59de-494d-ae40-6830fe5de543",
   "metadata": {},
   "source": [
    "基础的多组柱形图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "e88dcdce-e89e-481d-b57e-8ad97703d26d",
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyecharts.charts import Bar # 绘制条形图的模块\n",
    "from pyecharts import options as opts  # 配置项模块\n",
    "from pyecharts.faker import Faker # 用于生成假数据\n",
    "from pyecharts.globals import ThemeType # 主题配置项\n",
    "\n",
    "c = (\n",
    "    Bar({\"theme\":ThemeType.MACARONS})\n",
    "    .add_xaxis(Faker.choose())\n",
    "    .add_yaxis(\"男程序员A\",Faker.values())\n",
    "    .add_yaxis(\"女程序员B\",Faker.values())\n",
    "    .set_global_opts(   # 此时如果没有设置标题，那么默认标题就会显示在左侧\n",
    "        title_opts={\"text\":\"Bar-通过faker假数据进行配置\",\"subtext\":\"我也是通过faker假数据进行配置的\"}\n",
    "    ).render(\"bar_base假数据基础macar主题图表.html\")\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eabb6956-759a-4eac-98ad-553f5127b9a2",
   "metadata": {},
   "source": [
    "【6】"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "00d87ff0-584f-4292-b04b-fa942c282a76",
   "metadata": {},
   "source": [
    "柱形图与折线图的整合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "d30fe7fc-3596-4ea2-b845-5f55d86649d8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'D:\\\\jupyter\\\\线条与柱形图.html'"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from pyecharts.charts import Bar,Line\n",
    "from pyecharts import options as opts\n",
    "\n",
    "x_data = [\"1月\",\"2月\",\"3月\",\"4月\",\"5月\",\"6月\",\"7月\",\"8月\",\"9月\",\"10月\",\"11月\",\"12月\"]\n",
    "\n",
    "bar = (\n",
    "    Bar()\n",
    "    .add_xaxis(xaxis_data=x_data)\n",
    "    .add_yaxis(\n",
    "        series_name=\"蒸发量\",\n",
    "        y_axis=[2.0,4.9,7.0,23.5,25.6,45.6,9.0,4.8,15.3,19.1,35.5,40.3],\n",
    "        label_opts=opts.LabelOpts(is_show=False),\n",
    "        )\n",
    "    .add_yaxis(\n",
    "        series_name=\"降水量\",\n",
    "        y_axis=[2.9,4.9,8.9,23.8,25.9,45.0,9.9,4.5,15.9,19.0,35.8,40.3],\n",
    "        label_opts=opts.LabelOpts(is_show=False),\n",
    "        )\n",
    "    .extend_axis(\n",
    "        yaxis=opts.AxisOpts(  # 追加y轴\n",
    "            name='温度',\n",
    "            type_='value',\n",
    "            min_=0,\n",
    "            max_=25,\n",
    "            interval=5,\n",
    "            axislabel_opts=opts.LabelOpts(formatter=\"{value}摄氏度\")\n",
    "        )\n",
    "    )\n",
    "    .set_global_opts(\n",
    "        tooltip_opts=opts.TooltipOpts(\n",
    "            is_show=True,\n",
    "            trigger='axis',axis_pointer_type='cross'\n",
    "        ),\n",
    "        xaxis_opts=opts.AxisOpts(\n",
    "            type_=\"category\",\n",
    "            axispointer_opts=opts.AxisPointerOpts(is_show=True,type_=\"shadow\")\n",
    "        ),\n",
    "        yaxis_opts=opts.AxisOpts(\n",
    "            name=\"\",\n",
    "            type_=\"value\",\n",
    "            min_=0,\n",
    "            max_=250,\n",
    "            interval=50,\n",
    "            axislabel_opts=opts.LabelOpts(formatter=\"{value}ml\"),\n",
    "            axistick_opts=opts.AxisTickOpts(is_show=True),\n",
    "            splitline_opts=opts.SplitLineOpts(is_show=True)\n",
    "        )\n",
    "    )\n",
    ")    \n",
    "line = (\n",
    "    Line()\n",
    "    .add_xaxis(xaxis_data=x_data)\n",
    "    .add_yaxis(\n",
    "        series_name='平均温度',\n",
    "        yaxis_index=1,\n",
    "        y_axis=[2.0,2.2,2.3,3.3,4.5,6.3,10.2,20.3,23.4,23.0,16.5,12.6],\n",
    "        label_opts=opts.LabelOpts(is_show=False),\n",
    "    )\n",
    ")\n",
    " # 表示折线图在柱形图之上\n",
    "bar.overlap(line).render(\"线条与柱形图.html\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "91bd71ea-c57f-42ea-87e6-d6ac81b38ba3",
   "metadata": {},
   "source": [
    "【7】"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9b1ba252-92c7-4051-8419-e9bb3bc63335",
   "metadata": {},
   "source": [
    "堆叠柱形图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "2c14235a-c3a3-4a8c-ad13-42df76489a42",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>赛事</th>\n",
       "      <th>参赛人数</th>\n",
       "      <th>金牌</th>\n",
       "      <th>银牌</th>\n",
       "      <th>铜牌</th>\n",
       "      <th>总计</th>\n",
       "      <th>排名</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1984年洛杉矶</td>\n",
       "      <td>216</td>\n",
       "      <td>15</td>\n",
       "      <td>8</td>\n",
       "      <td>9</td>\n",
       "      <td>32</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1988年汉城</td>\n",
       "      <td>273</td>\n",
       "      <td>5</td>\n",
       "      <td>11</td>\n",
       "      <td>12</td>\n",
       "      <td>28</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1992年巴塞罗那</td>\n",
       "      <td>244</td>\n",
       "      <td>16</td>\n",
       "      <td>22</td>\n",
       "      <td>16</td>\n",
       "      <td>54</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1996年亚特兰大</td>\n",
       "      <td>294</td>\n",
       "      <td>16</td>\n",
       "      <td>22</td>\n",
       "      <td>12</td>\n",
       "      <td>50</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2000年悉尼</td>\n",
       "      <td>271</td>\n",
       "      <td>28</td>\n",
       "      <td>16</td>\n",
       "      <td>14</td>\n",
       "      <td>58</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>2004年雅典</td>\n",
       "      <td>384</td>\n",
       "      <td>32</td>\n",
       "      <td>17</td>\n",
       "      <td>14</td>\n",
       "      <td>63</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>2008年北京</td>\n",
       "      <td>639</td>\n",
       "      <td>48</td>\n",
       "      <td>22</td>\n",
       "      <td>30</td>\n",
       "      <td>100</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>2012年伦敦</td>\n",
       "      <td>396</td>\n",
       "      <td>38</td>\n",
       "      <td>31</td>\n",
       "      <td>22</td>\n",
       "      <td>91</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>2016年里约</td>\n",
       "      <td>412</td>\n",
       "      <td>26</td>\n",
       "      <td>18</td>\n",
       "      <td>26</td>\n",
       "      <td>70</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>2020年东京</td>\n",
       "      <td>431</td>\n",
       "      <td>38</td>\n",
       "      <td>32</td>\n",
       "      <td>18</td>\n",
       "      <td>88</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          赛事  参赛人数  金牌  银牌  铜牌   总计  排名\n",
       "0   1984年洛杉矶   216  15   8   9   32   4\n",
       "1    1988年汉城   273   5  11  12   28  11\n",
       "2  1992年巴塞罗那   244  16  22  16   54   4\n",
       "3  1996年亚特兰大   294  16  22  12   50   4\n",
       "4    2000年悉尼   271  28  16  14   58   3\n",
       "5    2004年雅典   384  32  17  14   63   2\n",
       "6    2008年北京   639  48  22  30  100   1\n",
       "7    2012年伦敦   396  38  31  22   91   2\n",
       "8    2016年里约   412  26  18  26   70   3\n",
       "9    2020年东京   431  38  32  18   88   2"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "data = pd.read_excel('./data/奖牌.xlsx')\n",
    "data"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "781a7429-a53e-49c8-8321-636b01ff0f58",
   "metadata": {},
   "source": [
    "单系列堆叠形状图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "1bed94b7-2a79-4cc0-bf75-a2d70c241001",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'D:\\\\jupyter\\\\历年奥运会奖牌数量堆叠柱形图.html'"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from pyecharts.charts import Bar # 绘制条形图的模块\n",
    "from pyecharts import options as opts  # 配置项模块\n",
    "from pyecharts.faker import Faker # 用于生成假数据\n",
    "from pyecharts.globals import ThemeType # 主题配置项\n",
    "import pandas as pd\n",
    "data = pd.read_excel('./data/奖牌.xlsx')\n",
    "\n",
    "# 计算各种奖牌占总数的比例\n",
    "data['glod_p'] = data['金牌'] / data['总计']\n",
    "data['silver_p'] = data['银牌'] / data['总计']\n",
    "data['bronze_p'] = data['铜牌'] / data['总计']\n",
    "\n",
    "# 提取数据\n",
    "dt1 = data.loc[:,['赛事','金牌','glod_p']].values.tolist()\n",
    "dt2 = data.loc[:,['赛事','银牌','silver_p']].values.tolist()\n",
    "dt3 = data.loc[:,['赛事','铜牌','bronze_p']].values.tolist()\n",
    "\n",
    "# 定义基础样式\n",
    "base_itemstyle = {\n",
    "    \"normal\":{\n",
    "                \"shadowOffsetY\":1,\n",
    "                \"shadowOffsetX\":4,\n",
    "                \"barBorderRadius\":[2,2,2,2],\n",
    "                \"opacity\":1,\n",
    "                \"shadowBlur\":20\n",
    "                    }\n",
    "}\n",
    "\n",
    "# 创建不同奖牌的样式\n",
    "itemstyle1 = base_itemstyle.copy()  # 复制基础样式\n",
    "itemstyle1['normal']['shadowColor'] = 'rgba(78,112,240,.5)'  # 设置金牌的阴影颜色\n",
    "\n",
    "itemstyle2 = base_itemstyle.copy()  # 复制基础样式\n",
    "itemstyle2['normal']['shadowColor'] = 'rgba(0,197,210,.5)' # 设置银牌的阴影颜色\n",
    "\n",
    "itemstyle3 = base_itemstyle.copy()  # 复制基础样式\n",
    "itemstyle3['normal']['shadowColor'] = 'rgba(255,206,43,.5)' # 设置铜牌的阴影颜色\n",
    "\n",
    "# 创建柱形图\n",
    "c = (\n",
    "    Bar(init_opts=opts.InitOpts(theme=ThemeType.LIGHT,bg_color='#ffffff')) # 初始化柱形图，设置主题和背景颜色\n",
    "    .add_xaxis([i[0] for i in dt1])  # 添加x轴数据，即赛事名称\n",
    "    .add_yaxis(\"金牌\",[i[1] for i in dt1],stack='stack1',category_gap='50%',itemstyle_opts=itemstyle1) # 添加金牌系列，设置类别间距和样式\n",
    "    .add_yaxis(\"银牌\",[i[1] for i in dt2],stack='stack1',category_gap='50%',itemstyle_opts=itemstyle2)\n",
    "    .add_yaxis(\"铜牌\",[i[1] for i in dt3],stack='stack1',category_gap='50%',itemstyle_opts=itemstyle3)\n",
    "    .set_series_opts(label_opts=opts.LabelOpts(position='top')) # 设置系列标签的位置\n",
    "    .set_global_opts(\n",
    "        title_opts=opts.TitleOpts(title='历年奥运会奖牌数量堆叠柱形图',pos_left='center',pos_top='2%'), # 设置图表标题及其位置\n",
    "        legend_opts=opts.LegendOpts(pos_top='7%'), # 设置图例的位置\n",
    "        xaxis_opts=opts.AxisOpts(name='赛事名称'), # 设置x轴的名称\n",
    "        toolbox_opts=opts.TooltipOpts(trigger='axis',axis_pointer_type='shadow'), # 设置提示框的触发方式和指针类型\n",
    "    )\n",
    "    \n",
    ")\n",
    "c.render('历年奥运会奖牌数量堆叠柱形图.html')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6c7430f9-a59f-4ca9-beff-57c6cdb42594",
   "metadata": {},
   "source": [
    "百分比堆叠柱状图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "e4bdf5aa-13f0-4121-925d-c7439d97279b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'D:\\\\jupyter\\\\历年奥运会中国奖牌数量百分比堆叠柱形图.html'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from pyecharts.charts import Bar # 绘制条形图的模块\n",
    "from pyecharts import options as opts  # 配置项模块\n",
    "from pyecharts.faker import Faker # 用于生成假数据\n",
    "from pyecharts.globals import ThemeType,JsCode\n",
    "import pandas as pd\n",
    "data = pd.read_excel('./data/奖牌.xlsx')\n",
    "\n",
    "# 计算各种奖牌占总数的比例\n",
    "data['glod_p'] = data['金牌'] / data['总计']\n",
    "data['silver_p'] = data['银牌'] / data['总计']\n",
    "data['bronze_p'] = data['铜牌'] / data['总计']\n",
    "\n",
    "# 提取数据\n",
    "dt1 = data.loc[:,['赛事','金牌','glod_p']].values.tolist()\n",
    "dt2 = data.loc[:,['赛事','银牌','silver_p']].values.tolist()\n",
    "dt3 = data.loc[:,['赛事','铜牌','bronze_p']].values.tolist()\n",
    "\n",
    "# 定义基础样式\n",
    "base_itemstyle = {\n",
    "    \"normal\":{\n",
    "                \"shadowOffsetY\":1,\n",
    "                \"shadowOffsetX\":4,\n",
    "                \"barBorderRadius\":[2,2,2,2],\n",
    "                \"opacity\":1,\n",
    "                \"shadowBlur\":20\n",
    "                    }\n",
    "}\n",
    "\n",
    "# 创建不同奖牌的样式\n",
    "itemstyle1 = base_itemstyle.copy()  # 复制基础样式\n",
    "itemstyle1['normal']['shadowColor'] = 'rgba(78,112,240,.5)'  # 设置金牌的阴影颜色\n",
    "\n",
    "itemstyle2 = base_itemstyle.copy()  # 复制基础样式\n",
    "itemstyle2['normal']['shadowColor'] = 'rgba(0,197,210,.5)' # 设置银牌的阴影颜色\n",
    "\n",
    "itemstyle3 = base_itemstyle.copy()  # 复制基础样式\n",
    "itemstyle3['normal']['shadowColor'] = 'rgba(255,206,43,.5)' # 设置铜牌的阴影颜色\n",
    "\n",
    "# 数据标签格式化函数\n",
    "jscode = \"function(x){return Number(x.data * 100).toFixed(1) + '%';}\"\n",
    "\n",
    "# 坐标轴标签格式化函数\n",
    "jscode1 = \"function(x){return Number(x * 100).toFixed(1) + '%';}\"\n",
    "'''\n",
    "这段 JavaScript 代码通常用于在 pyecharts 生成的图表中自定义某些行为，比如格式化数据标签、坐标轴标签等\n",
    "这段代码定义了一个 JavaScript 函数，该函数接收一个参数 x（在 pyecharts 的上下文中，这通常是一个包含当前数据点信息的对象），\n",
    "并返回一个格式化后的字符串，该字符串表示 x.data 的值乘以 100 并保留一位小数的百分比形式。\n",
    "jscode 是一个包含自定义 JavaScript 代码的字符串，它通过 JsCode 类被嵌入到 pyecharts 图表的配置中，\n",
    "以实现自定义的图表行为\n",
    "'''\n",
    "# 创建柱形图\n",
    "c = (\n",
    "    Bar(init_opts=opts.InitOpts(theme=ThemeType.LIGHT,bg_color='#ffffff')) # 初始化柱形图，设置主题和背景颜色\n",
    "    .add_xaxis([i[0] for i in dt1])  # 添加x轴数据，即赛事名称\n",
    "    .add_yaxis(\"金牌\",[round(i[2] ,2) for i in dt1],stack='stack1',category_gap='55%',itemstyle_opts=itemstyle1,\n",
    "               label_opts=opts.LabelOpts(position=\"right\",formatter=JsCode(jscode),color=\"#4e71f1\"))   # 添加金牌系列 \n",
    "    .add_yaxis(\"银牌\",[round(i[2],2) for i in dt2],stack='stack1',category_gap='55%',itemstyle_opts=itemstyle2,\n",
    "               label_opts=opts.LabelOpts(position=\"right\",formatter=JsCode(jscode),color=\"#00c5d2\"))   # 添加银牌系列\n",
    "    .add_yaxis(\"铜牌\",[round(i[2],2) for i in dt3],stack='stack1',category_gap='55%',itemstyle_opts=itemstyle3,\n",
    "               label_opts=opts.LabelOpts(position=\"right\",formatter=JsCode(jscode),color=\"#ffce2b\"))   # 添加铜牌系列\n",
    "    .set_global_opts(\n",
    "        title_opts=opts.TitleOpts(title='历年奥运会中国奖牌数量百分比堆叠柱形图',pos_left='center',pos_top='2%'), # 设置图表标题及其位置\n",
    "        xaxis_opts=opts.AxisOpts(name='赛事名称',axislabel_opts=opts.LabelOpts(rotate=15)), # 设置x轴选项\n",
    "        toolbox_opts=opts.TooltipOpts(trigger='axis',axis_pointer_type='shadow'), # 设置提示框的触发方式和指针类型\n",
    "        # 设置y轴分割线的显示和样式\n",
    "        yaxis_opts=opts.AxisOpts(max_=1,axislabel_opts=opts.LabelOpts(formatter=JsCode(jscode1))) # 设置y轴选项\n",
    "    )\n",
    "    \n",
    ")\n",
    "c.render('历年奥运会中国奖牌数量百分比堆叠柱形图.html')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e39542f5-d40f-4170-911d-4753ea19afec",
   "metadata": {},
   "source": [
    "【8】"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "910c4e83-8750-492e-b0a9-9b90780da06b",
   "metadata": {},
   "source": [
    "优化选项提示ToolTip"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "8f1ab47a-4e2d-4795-964e-4eacc4b05437",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'D:\\\\jupyter\\\\历年奥运会中国奖牌数量百分比堆叠柱形图.html'"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from pyecharts.charts import Bar # 绘制条形图的模块\n",
    "from pyecharts import options as opts  # 配置项模块\n",
    "from pyecharts.faker import Faker # 用于生成假数据\n",
    "from pyecharts.globals import ThemeType,JsCode\n",
    "import pandas as pd\n",
    "data = pd.read_excel('./data/奖牌.xlsx')\n",
    "\n",
    "# 计算各种奖牌占总数的比例\n",
    "data['glod_p'] = data['金牌'] / data['总计']\n",
    "data['silver_p'] = data['银牌'] / data['总计']\n",
    "data['bronze_p'] = data['铜牌'] / data['总计']\n",
    "\n",
    "# 提取数据\n",
    "dt1 = data.loc[:,['赛事','金牌','glod_p']].values.tolist()\n",
    "dt2 = data.loc[:,['赛事','银牌','silver_p']].values.tolist()\n",
    "dt3 = data.loc[:,['赛事','铜牌','bronze_p']].values.tolist()\n",
    "\n",
    "# 定义基础样式\n",
    "base_itemstyle = {\n",
    "    \"normal\":{\n",
    "                \"shadowOffsetY\":1,\n",
    "                \"shadowOffsetX\":4,\n",
    "                \"barBorderRadius\":[2,2,2,2],\n",
    "                \"opacity\":1,\n",
    "                \"shadowBlur\":20\n",
    "                    }\n",
    "}\n",
    "\n",
    "# 创建不同奖牌的样式\n",
    "itemstyle1 = base_itemstyle.copy()  # 复制基础样式\n",
    "itemstyle1['normal']['shadowColor'] = 'rgba(78,112,240,.5)'  # 设置金牌的阴影颜色\n",
    "\n",
    "itemstyle2 = base_itemstyle.copy()  # 复制基础样式\n",
    "itemstyle2['normal']['shadowColor'] = 'rgba(0,197,210,.5)' # 设置银牌的阴影颜色\n",
    "\n",
    "itemstyle3 = base_itemstyle.copy()  # 复制基础样式\n",
    "itemstyle3['normal']['shadowColor'] = 'rgba(255,206,43,.5)' # 设置铜牌的阴影颜色\n",
    "\n",
    "# 创建柱形图\n",
    "c = (\n",
    "    Bar(init_opts=opts.InitOpts(theme=ThemeType.LIGHT,bg_color='#ffffff')) # 初始化柱形图，设置主题和背景颜色\n",
    "    .add_xaxis([i[0] for i in dt1])  # 添加x轴数据，即赛事名称\n",
    "    .add_yaxis(\"金牌\",[round(i[2] * 100 ,1) for i in dt1],stack='stack1',category_gap='55%',itemstyle_opts=itemstyle1,\n",
    "               label_opts=opts.LabelOpts(position=\"right\",formatter=\"{c}%\"))   # 添加金牌系列 \n",
    "    .add_yaxis(\"银牌\",[round(i[2] * 100 ,1) for i in dt2],stack='stack1',category_gap='55%',itemstyle_opts=itemstyle2,\n",
    "               label_opts=opts.LabelOpts(position=\"right\",formatter=\"{c}%\"))   # 添加银牌系列\n",
    "    .add_yaxis(\"铜牌\",[round(i[2] * 100 ,1) for i in dt3],stack='stack1',category_gap='55%',itemstyle_opts=itemstyle3,\n",
    "               label_opts=opts.LabelOpts(position=\"right\",formatter=\"{c}%\"))   # 添加铜牌系列\n",
    "    .set_global_opts(\n",
    "        title_opts=opts.TitleOpts(title='历年奥运会中国奖牌数量百分比堆叠柱形图',pos_left='center',pos_top='2%'), # 设置图表标题及其位置\n",
    "        xaxis_opts=opts.AxisOpts(name='赛事名称',axislabel_opts=opts.LabelOpts(rotate=15)), # 设置x轴选项\n",
    "        # 设置y轴分割线的显示和样式\n",
    "        yaxis_opts=opts.AxisOpts(max_=100,axislabel_opts=opts.LabelOpts(formatter=\"{value}%\")), # 设置y轴选项\n",
    "        tooltip_opts=opts.TooltipOpts(\n",
    "            trigger='axis',\n",
    "            axis_pointer_type='shadow',\n",
    "            formatter=JsCode(\n",
    "                \"function (params) {\"\n",
    "                \"var result = params[0].name + '<br/>';\"\n",
    "                \"for (var i=0; i < params.length;i++)  {\"\n",
    "                \"result += params[i].seriesName + ':' +params[i].value + '%<br/>';\"\n",
    "                \"}\"\n",
    "                \"return result;\"\n",
    "                \"}\"\n",
    "            )\n",
    "        )\n",
    "    )\n",
    "    \n",
    ")\n",
    "c.render('历年奥运会中国奖牌数量百分比堆叠柱形图.html')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "751c9eb0-5264-4d17-8f08-bd0c95341c3a",
   "metadata": {},
   "source": [
    "【9】"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "03b75805-123c-4a6b-aa00-d3cee6c396fc",
   "metadata": {},
   "source": [
    "水平柱形图"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "158b1573-4ab3-45ed-aade-74d49da73915",
   "metadata": {},
   "source": [
    "单数据水平柱形图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "a31e1811-9a5a-4490-b124-a1194685bf1a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[['1984年洛杉矶', 216],\n",
       " ['1992年巴塞罗那', 244],\n",
       " ['2000年悉尼', 271],\n",
       " ['1988年汉城', 273],\n",
       " ['1996年亚特兰大', 294],\n",
       " ['2004年雅典', 384],\n",
       " ['2012年伦敦', 396],\n",
       " ['2016年里约', 412],\n",
       " ['2020年东京', 431],\n",
       " ['2008年北京', 639]]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "from pyecharts.charts import Bar\n",
    "from pyecharts import options as opts\n",
    "from pyecharts.globals import ThemeType\n",
    "\n",
    "data = pd.read_excel('./data/奖牌.xlsx')\n",
    "# 读取并排序数据\n",
    "dt = data.loc[:,['赛事','参赛人数']].sort_values(by='参赛人数').values.tolist()\n",
    "dt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "8b3d7471-d3d7-48c5-8273-6086c1d96097",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'D:\\\\jupyter\\\\水平柱形图.html'"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "from pyecharts.charts import Bar\n",
    "from pyecharts import options as opts\n",
    "from pyecharts.globals import ThemeType\n",
    "\n",
    "data = pd.read_excel('./data/奖牌.xlsx')\n",
    "# 读取并排序数据\n",
    "dt = data.loc[:,['赛事','参赛人数']].sort_values(by='参赛人数').values.tolist()\n",
    "# 定义基础样式\n",
    "base_itemstyle = {\n",
    "    \"normal\":{\n",
    "        \"color\":\"#00c5d2\",\n",
    "        \"shadowBlur\":4,\n",
    "        \"shadowOffSetY\":4,\n",
    "        \"shadowOffSetX\":4,\n",
    "        \"shadowColor\":'rgba(78,112,240,.5)',\n",
    "        \"barBorderRadius\":[2,2,2,2],\n",
    "        \"opacity\":1\n",
    "    }\n",
    "}\n",
    "\n",
    "# 创建水平柱形图\n",
    "bar = (\n",
    "    Bar(init_opts=opts.InitOpts(theme=ThemeType.LIGHT)) # 初始化柱形图，设置主题\n",
    "    .add_xaxis([i[0][:5] for i in dt])  # 添加x轴数据（年份）\n",
    "    .add_yaxis('参赛人员',[i[1] for i in dt],itemstyle_opts=base_itemstyle) # 添加y轴数据（参赛人员）\n",
    "    .reversal_axis() # 反转轴，使图表变成水平柱形图\n",
    "    .set_series_opts(\n",
    "        label_opts=opts.LabelOpts(\n",
    "            position=\"right\",\n",
    "            formatter=\"{b}:{c}人\",\n",
    "            color=\"#00c5d2\" # 设置标签文字的颜色\n",
    "        )\n",
    "    )\n",
    "    .set_global_opts(\n",
    "        title_opts=opts.TitleOpts(title='历届奥运会中国参赛人数'),\n",
    "        xaxis_opts=opts.AxisOpts(name='参赛人数'),\n",
    "        yaxis_opts=opts.AxisOpts(name='赛事')\n",
    "    )\n",
    ")\n",
    "\n",
    "bar.render('水平柱形图.html')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "da905b41-bc81-4142-ad1c-e85059a4ac05",
   "metadata": {},
   "source": [
    "难道说加上'.reversal_axis() # 反转轴，使图表变成水平柱形图'就能变成"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "600c491b-6981-441b-a6e4-7c167613d143",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\abc18\\AppData\\Local\\Temp\\ipykernel_23812\\517158486.py:6: FutureWarning: The argument 'date_parser' is deprecated and will be removed in a future version. Please use 'date_format' instead, or read your data in as 'object' dtype and then call 'to_datetime'.\n",
      "  data = pd.read_excel('./data/subject.xlsx',date_parser='Month')\n",
      "C:\\Users\\abc18\\AppData\\Local\\Temp\\ipykernel_23812\\517158486.py:8: FutureWarning: 'Y' is deprecated and will be removed in a future version, please use 'YE' instead.\n",
      "  data_y = data_AIeye.set_index('Month').resample('Y').sum().astype(int)['Gain'].tolist()\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'D:\\\\jupyter\\\\kagge竞赛非职业人员水平柱形图.html'"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from pyecharts.charts import Bar # 绘制条形图的模块\n",
    "from pyecharts import options as opts  # 配置项模块\n",
    "from pyecharts.faker import Faker # 用于生成假数据\n",
    "from pyecharts.globals import ThemeType # 主题配置项\n",
    "import pandas as pd\n",
    "data = pd.read_excel('./data/subject.xlsx',date_parser='Month')\n",
    "data_AIeye = data[data['Name']=='AI-视觉'][['Month','Gain']]  # 按年对数据进行重采用\n",
    "data_y = data_AIeye.set_index('Month').resample('Y').sum().astype(int)['Gain'].tolist()\n",
    "# 计算之后，转为int后将其转化为列表\n",
    "data_x = data_AIeye.loc[:,'Month'].dt.year.unique().tolist()  # 将年份单独取出转换为list\n",
    "\n",
    "bar = (Bar(init_opts=opts.InitOpts(theme=ThemeType.DARK,bg_color='#080b30'))\n",
    "      .add_xaxis(data_x)  # 添加x轴信息，传入列表，元组序列格式\n",
    "      .add_yaxis('年增长人数',data_y) # 第一个参数系列名称，第二个参数是要显示的数据\n",
    "      .set_global_opts(title_opts=opts.TitleOpts(\n",
    "          title='AI-视觉 kaggel国家竞赛 非职业人员 年增长人数柱形图',\n",
    "          pos_left='center'),# 标题配置项，设置标题以及标题的位置，此处表示标题居中，默认是居左\n",
    "        legend_opts=opts.LegendOpts(pos_top='5%'), # 图例设置，图例的位置\n",
    "        tooltip_opts=opts.TooltipOpts(trigger='axis',\n",
    "                                     axis_pointer_type='shadow')) # 提示框组件配置，显示样式为阴影\n",
    "       .reversal_axis() # 反转轴，使图表变成水平柱形图\n",
    ")\n",
    "bar.render(\"kagge竞赛非职业人员水平柱形图.html\") # 渲染图表到网页"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "73ab2207-1420-40ec-bf00-61c7c053ed55",
   "metadata": {},
   "source": [
    "多数据系列水平图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "21b97c35-40c1-4db3-9a84-8587a91a8e61",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'D:\\\\jupyter\\\\多数据系列水平柱形图.html'"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "from pyecharts.charts import Bar\n",
    "from pyecharts import options as opts\n",
    "from pyecharts.globals import ThemeType\n",
    "\n",
    "data = pd.read_excel('./data/奖牌.xlsx')\n",
    "# 提取近五年的赛事信息\n",
    "dt1 = data.loc[:,['赛事','金牌']].values.tolist()[-5:]\n",
    "dt2 = data.loc[:,['赛事','银牌']].values.tolist()[-5:]\n",
    "dt3 = data.loc[:,['赛事','铜牌']].values.tolist()[-5:]\n",
    "\n",
    "# 定义基础样式\n",
    "base_itemstyle = {\n",
    "    \"normal\":{\n",
    "        \"color\":\"#00c5d2\",\n",
    "        \"shadowBlur\":4,\n",
    "        \"shadowOffSetY\":4,\n",
    "        \"shadowOffSetX\":4,\n",
    "        \"shadowColor\":'rgba(78,112,240,.5)',\n",
    "        \"barBorderRadius\":[2,2,2,2],\n",
    "        \"opacity\":1\n",
    "    }\n",
    "}\n",
    "\n",
    "# 创建不同奖牌的样式\n",
    "itemstyle1 = base_itemstyle.copy()  # 复制基础样式\n",
    "itemstyle1['normal']['shadowColor'] = 'rgba(78,112,240,.5)'  # 设置金牌的阴影颜色\n",
    "\n",
    "itemstyle2 = base_itemstyle.copy()  # 复制基础样式\n",
    "itemstyle2['normal']['shadowColor'] = 'rgba(0,197,210,.5)' # 设置银牌的阴影颜色\n",
    "\n",
    "itemstyle3 = base_itemstyle.copy()  # 复制基础样式\n",
    "itemstyle3['normal']['shadowColor'] = 'rgba(255,206,43,.5)' # 设置铜牌的阴影颜色\n",
    "\n",
    "# 创建水平柱状图\n",
    "bar = (\n",
    "    Bar(init_opts=opts.InitOpts(theme=ThemeType.LIGHT))   # 初始化柱状图\n",
    "    .add_xaxis([i[0] for i in dt1])  # 添加x轴数据\n",
    "    .add_yaxis(\n",
    "        \"金牌\",\n",
    "        [i[1] for i in dt1],\n",
    "        category_gap='30%',\n",
    "        itemstyle_opts=itemstyle1,\n",
    "        label_opts=opts.LabelOpts(position=\"right\",formatter=\"{b},{c}枚\",color=\"#4e70f0\") \n",
    "    )\n",
    "    .add_yaxis(\n",
    "        \"银牌\",\n",
    "        [i[1] for i in dt2],\n",
    "        category_gap='30%',\n",
    "        itemstyle_opts=itemstyle2,\n",
    "        label_opts=opts.LabelOpts(position=\"right\",formatter=\"{b},{c}枚\",color=\"#00c5d2\") \n",
    "    )\n",
    "    .add_yaxis(\n",
    "        \"铜牌\",\n",
    "        [i[1] for i in dt3],\n",
    "        category_gap='30%',\n",
    "        itemstyle_opts=itemstyle3,\n",
    "        label_opts=opts.LabelOpts(position=\"right\",formatter=\"{b},{c}枚\",color=\"#ffce2b\") \n",
    "    )\n",
    "    .reversal_axis()\n",
    "    .set_global_opts(\n",
    "        title_opts=opts.TooltipOpts(trigger='axis',axis_pointer_type='shadow')\n",
    "    )\n",
    ")\n",
    "bar.render('多数据系列水平柱形图.html')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4f9410e8-f8fa-4840-a0c5-c1acaed21e2a",
   "metadata": {},
   "source": [
    "【10】"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "737fe086-5c2a-4287-a077-f9430446cf9c",
   "metadata": {},
   "source": [
    "水平堆叠柱形图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "5a3402de-e800-4695-a8c9-d45f30eebbb4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'D:\\\\jupyter\\\\历年奥运会奖牌数量堆叠水平柱形图.html'"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from pyecharts.charts import Bar # 绘制条形图的模块\n",
    "from pyecharts import options as opts  # 配置项模块\n",
    "from pyecharts.faker import Faker # 用于生成假数据\n",
    "from pyecharts.globals import ThemeType # 主题配置项\n",
    "import pandas as pd\n",
    "data = pd.read_excel('./data/奖牌.xlsx')\n",
    "\n",
    "# 提取近6年的赛事信息\n",
    "dt1 = data.loc[:,['赛事','金牌']].values.tolist()[-6:]\n",
    "dt2 = data.loc[:,['赛事','银牌']].values.tolist()[-6:]\n",
    "dt3 = data.loc[:,['赛事','铜牌']].values.tolist()[-6:]\n",
    "\n",
    "# 定义基础样式\n",
    "base_itemstyle = {\n",
    "    \"normal\":{\n",
    "                \"shadowOffsetY\":1,\n",
    "                \"shadowOffsetX\":4,\n",
    "                \"barBorderRadius\":[2,2,2,2],\n",
    "                \"opacity\":1,\n",
    "                \"shadowBlur\":20\n",
    "                    }\n",
    "}\n",
    "\n",
    "# 创建不同奖牌的样式\n",
    "itemstyle1 = base_itemstyle.copy()  # 复制基础样式\n",
    "itemstyle1['normal']['shadowColor'] = 'rgba(78,112,240,.5)'  # 设置金牌的阴影颜色\n",
    "\n",
    "itemstyle2 = base_itemstyle.copy()  # 复制基础样式\n",
    "itemstyle2['normal']['shadowColor'] = 'rgba(0,197,210,.5)' # 设置银牌的阴影颜色\n",
    "\n",
    "itemstyle3 = base_itemstyle.copy()  # 复制基础样式\n",
    "itemstyle3['normal']['shadowColor'] = 'rgba(255,206,43,.5)' # 设置铜牌的阴影颜色\n",
    "\n",
    "# 创建柱形图\n",
    "c = (\n",
    "    Bar(init_opts=opts.InitOpts(theme=ThemeType.LIGHT,bg_color='#ffffff')) # 初始化柱形图，设置主题和背景颜色\n",
    "    .add_xaxis([i[0] for i in dt1])  # 添加x轴数据\n",
    "    .add_yaxis(\"金牌\",[i[1] for i in dt1],stack='stack1',category_gap='50%',itemstyle_opts=itemstyle1,\n",
    "              label_opts=opts.LabelOpts(position='inside',formatter=\"{a}:{c}枚\",color='#ffffff')) # 添加金牌系列，设置类别间距和样式\n",
    "    .add_yaxis(\"银牌\",[i[1] for i in dt2],stack='stack1',category_gap='50%',itemstyle_opts=itemstyle2,\n",
    "              label_opts=opts.LabelOpts(position='inside',formatter=\"{a}:{c}枚\",color='#ffffff'))\n",
    "    .add_yaxis(\"铜牌\",[i[1] for i in dt3],stack='stack1',category_gap='50%',itemstyle_opts=itemstyle3,\n",
    "              label_opts=opts.LabelOpts(position='inside',formatter=\"{a}:{c}枚\",color='#000000'))\n",
    "    .reversal_axis()  # 反转轴, 使图表变成水平柱形图\n",
    "    #.set_series_opts(label_opts=opts.LabelOpts(position='top',formatter=\"{a}:{c}枚\",color='#ffffff')) # 设置系列标签的位置和格式\n",
    "    .set_global_opts(\n",
    "        title_opts=opts.TitleOpts(title='历年奥运会奖牌数量堆叠水平柱形图',pos_left='center',pos_top='2%'), # 设置图表标题及其位置\n",
    "        tooltip_opts=opts.TooltipOpts(trigger='axis',axis_pointer_type='shadow') # 设置提示框\n",
    "    )\n",
    "    \n",
    ")\n",
    "c.render('历年奥运会奖牌数量堆叠水平柱形图.html')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "0c976b2f-8ae6-4989-9c54-99c9e904a26c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'D:\\\\jupyter\\\\历年奥运会奖牌数量堆叠水平柱形图1.html'"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from pyecharts.charts import Bar # 绘制条形图的模块\n",
    "from pyecharts import options as opts  # 配置项模块\n",
    "from pyecharts.faker import Faker # 用于生成假数据\n",
    "from pyecharts.globals import ThemeType # 主题配置项\n",
    "import pandas as pd\n",
    "data = pd.read_excel('./data/奖牌.xlsx')\n",
    "\n",
    "# 计算各种奖牌占总数的比例\n",
    "data['glod_p'] = data['金牌'] / data['总计']\n",
    "data['silver_p'] = data['银牌'] / data['总计']\n",
    "data['bronze_p'] = data['铜牌'] / data['总计']\n",
    "\n",
    "# 提取数据\n",
    "dt1 = data.loc[:,['赛事','金牌','glod_p']].values.tolist()\n",
    "dt2 = data.loc[:,['赛事','银牌','silver_p']].values.tolist()\n",
    "dt3 = data.loc[:,['赛事','铜牌','bronze_p']].values.tolist()\n",
    "\n",
    "# 定义基础样式\n",
    "base_itemstyle = {\n",
    "    \"normal\":{\n",
    "                \"shadowOffsetY\":1,\n",
    "                \"shadowOffsetX\":4,\n",
    "                \"barBorderRadius\":[2,2,2,2],\n",
    "                \"opacity\":1,\n",
    "                \"shadowBlur\":20\n",
    "                    }\n",
    "}\n",
    "\n",
    "# 创建不同奖牌的样式\n",
    "itemstyle1 = base_itemstyle.copy()  # 复制基础样式\n",
    "itemstyle1['normal']['shadowColor'] = 'rgba(78,112,240,.5)'  # 设置金牌的阴影颜色\n",
    "\n",
    "itemstyle2 = base_itemstyle.copy()  # 复制基础样式\n",
    "itemstyle2['normal']['shadowColor'] = 'rgba(0,197,210,.5)' # 设置银牌的阴影颜色\n",
    "\n",
    "itemstyle3 = base_itemstyle.copy()  # 复制基础样式\n",
    "itemstyle3['normal']['shadowColor'] = 'rgba(255,206,43,.5)' # 设置铜牌的阴影颜色\n",
    "\n",
    "# 创建柱形图\n",
    "c = (\n",
    "    Bar(init_opts=opts.InitOpts(theme=ThemeType.LIGHT,bg_color='#ffffff')) # 初始化柱形图，设置主题和背景颜色\n",
    "    .add_xaxis([i[0] for i in dt1])  # 添加x轴数据，即赛事名称\n",
    "    .add_yaxis(\"金牌\",[i[1] for i in dt1],stack='stack1',category_gap='50%',itemstyle_opts=itemstyle1) # 添加金牌系列，设置类别间距和样式\n",
    "    .add_yaxis(\"银牌\",[i[1] for i in dt2],stack='stack1',category_gap='50%',itemstyle_opts=itemstyle2)\n",
    "    .add_yaxis(\"铜牌\",[i[1] for i in dt3],stack='stack1',category_gap='50%',itemstyle_opts=itemstyle3)\n",
    "    .set_series_opts(label_opts=opts.LabelOpts(position='top')) # 设置系列标签的位置\n",
    "    .reversal_axis()\n",
    "    .set_global_opts(\n",
    "        title_opts=opts.TitleOpts(title='历年奥运会奖牌数量堆叠水平柱形图',pos_left='center',pos_top='2%'), # 设置图表标题及其位置\n",
    "        legend_opts=opts.LegendOpts(pos_top='7%'), # 设置图例的位置\n",
    "        xaxis_opts=opts.AxisOpts(name='赛事名称'), # 设置x轴的名称\n",
    "        toolbox_opts=opts.TooltipOpts(trigger='axis',axis_pointer_type='shadow'), # 设置提示框的触发方式和指针类型\n",
    "    )\n",
    "    \n",
    ")\n",
    "c.render('历年奥运会奖牌数量堆叠水平柱形图1.html')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "76bc1fac-ccdf-4243-abd6-1fde6ed1039d",
   "metadata": {},
   "source": [
    "水平堆叠百分比柱形图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "3f52d5d3-1360-4fbe-93ec-98dbab2a0509",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'D:\\\\jupyter\\\\百分比水平堆叠柱形图.html'"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "from pyecharts import options as opts\n",
    "from pyecharts.charts import Bar\n",
    "from pyecharts.globals import ThemeType\n",
    "\n",
    "data = pd.read_excel('./data/奖牌.xlsx')\n",
    "# 计算每种奖牌的比例\n",
    "data['gold_p'] = (data['金牌']/data['总计'])*100\n",
    "data['sliver_p'] = (data['银牌']/data['总计'])*100\n",
    "data['bronze_p'] = (data['铜牌']/data['总计'])*100\n",
    "\n",
    "# 提取需要的数据列\n",
    "dt1 = data.loc[:,['赛事','金牌','gold_p']].values.tolist()\n",
    "dt2 = data.loc[:,['赛事','银牌','sliver_p']].values.tolist()\n",
    "dt3 = data.loc[:,['赛事','铜牌','bronze_p']].values.tolist()\n",
    "\n",
    "# 定义基础样式\n",
    "base_itemstyle = {\n",
    "    \"normal\":{\n",
    "                \"shadowOffsetY\":2,\n",
    "                \"shadowOffsetX\":2,\n",
    "                \"barBorderRadius\":[2,2,2,2],\n",
    "                \"opacity\":1,\n",
    "                \"shadowBlur\":2\n",
    "                    }\n",
    "}\n",
    "# 创建不同奖牌的样式\n",
    "itemstyle1 = base_itemstyle.copy()  # 复制基础样式\n",
    "itemstyle1['normal']['shadowColor'] = 'rgba(78,112,240,.5)'  # 设置金牌的阴影颜色\n",
    "\n",
    "itemstyle2 = base_itemstyle.copy()  # 复制基础样式\n",
    "itemstyle2['normal']['shadowColor'] = 'rgba(0,197,210,.5)' # 设置银牌的阴影颜色\n",
    "\n",
    "itemstyle3 = base_itemstyle.copy()  # 复制基础样式\n",
    "itemstyle3['normal']['shadowColor'] = 'rgba(255,206,43,.5)' # 设置铜牌的阴影颜色\n",
    "\n",
    "# 创建水平堆叠柱形图\n",
    "c = (\n",
    "    Bar(init_opts=opts.InitOpts(theme=ThemeType.LIGHT))\n",
    "    .add_xaxis([i[0] for i in dt1]) # 添加x轴数据（年份）\n",
    "    .add_yaxis(\"金牌\",[round(i[2] ,2) for i in dt1],stack='stack1',category_gap='55%',itemstyle_opts=itemstyle1,\n",
    "               label_opts=opts.LabelOpts(position=\"right\",formatter=\"{c}%\",color=\"#4e71f1\"))   # 添加金牌系列 \n",
    "    .add_yaxis(\"银牌\",[round(i[2],2) for i in dt2],stack='stack1',category_gap='55%',itemstyle_opts=itemstyle2,\n",
    "               label_opts=opts.LabelOpts(position=\"right\",formatter=\"{c}%\",color=\"#00c5d2\"))   # 添加银牌系列\n",
    "    .add_yaxis(\"铜牌\",[round(i[2],2) for i in dt3],stack='stack1',category_gap='55%',itemstyle_opts=itemstyle3,\n",
    "               label_opts=opts.LabelOpts(position=\"right\",formatter=\"{c}%\",color=\"#ffce2b\"))   # 添加铜牌系列\n",
    "    .reversal_axis()\n",
    "    .set_series_opts(label_opts=opts.LabelOpts(position=\"right\",formatter=\"{c}%\")) # 设置系统标签的位置和格式\n",
    "    .set_global_opts(\n",
    "        title_opts=opts.TitleOpts(title='',pos_left='center',pos_top='2%'), # 设置图表标题\n",
    "        legend_opts=opts.LegendOpts(pos_top='7%'), # 设置图例位置\n",
    "        tooltip_opts=opts.TooltipOpts(\n",
    "            trigger='axis',\n",
    "            axis_pointer_type='shadow',\n",
    "            formatter=\"{b}<br/>{a0}: {c0}<br/{a2}>: {c2}%\"\n",
    "        ),\n",
    "        yaxis_opts=opts.AxisOpts(name='',axislabel_opts=opts.LabelOpts(rotate=20)), # 坐标轴标签旋转\n",
    "        xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(position='inside',formatter=\"{value}%\"),max_=100)\n",
    "    )\n",
    ")\n",
    "\n",
    "c.render(\"百分比水平堆叠柱形图.html\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e69d3a94-d6a2-4ade-824f-a3cce3e8e384",
   "metadata": {},
   "source": [
    "反向把水平变为垂直"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "6ddec787-7ce9-499a-a6e9-95386497d9c0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'D:\\\\jupyter\\\\百分比堆叠柱形图.html'"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "from pyecharts import options as opts\n",
    "from pyecharts.charts import Bar\n",
    "from pyecharts.globals import ThemeType\n",
    "\n",
    "data = pd.read_excel('./data/奖牌.xlsx')\n",
    "# 计算每种奖牌的比例\n",
    "data['gold_p'] = (data['金牌']/data['总计'])*100\n",
    "data['sliver_p'] = (data['银牌']/data['总计'])*100\n",
    "data['bronze_p'] = (data['铜牌']/data['总计'])*100\n",
    "\n",
    "# 提取需要的数据列\n",
    "dt1 = data.loc[:,['赛事','金牌','gold_p']].values.tolist()\n",
    "dt2 = data.loc[:,['赛事','银牌','sliver_p']].values.tolist()\n",
    "dt3 = data.loc[:,['赛事','铜牌','bronze_p']].values.tolist()\n",
    "\n",
    "# 定义基础样式\n",
    "base_itemstyle = {\n",
    "    \"normal\":{\n",
    "                \"shadowOffsetY\":2,\n",
    "                \"shadowOffsetX\":2,\n",
    "                \"barBorderRadius\":[2,2,2,2],\n",
    "                \"opacity\":1,\n",
    "                \"shadowBlur\":2\n",
    "                    }\n",
    "}\n",
    "# 创建不同奖牌的样式\n",
    "itemstyle1 = base_itemstyle.copy()  # 复制基础样式\n",
    "itemstyle1['normal']['shadowColor'] = 'rgba(78,112,240,.5)'  # 设置金牌的阴影颜色\n",
    "\n",
    "itemstyle2 = base_itemstyle.copy()  # 复制基础样式\n",
    "itemstyle2['normal']['shadowColor'] = 'rgba(0,197,210,.5)' # 设置银牌的阴影颜色\n",
    "\n",
    "itemstyle3 = base_itemstyle.copy()  # 复制基础样式\n",
    "itemstyle3['normal']['shadowColor'] = 'rgba(255,206,43,.5)' # 设置铜牌的阴影颜色\n",
    "\n",
    "# 创建水平堆叠柱形图\n",
    "c = (\n",
    "    Bar(init_opts=opts.InitOpts(theme=ThemeType.LIGHT))\n",
    "    .add_xaxis([i[0] for i in dt1]) # 添加x轴数据（年份）\n",
    "    .add_yaxis(\"金牌\",[round(i[2] ,2) for i in dt1],stack='stack1',category_gap='55%',itemstyle_opts=itemstyle1,\n",
    "               label_opts=opts.LabelOpts(position=\"right\",formatter=\"{c}%\",color=\"#4e71f1\"))   # 添加金牌系列 \n",
    "    .add_yaxis(\"银牌\",[round(i[2],2) for i in dt2],stack='stack1',category_gap='55%',itemstyle_opts=itemstyle2,\n",
    "               label_opts=opts.LabelOpts(position=\"right\",formatter=\"{c}%\",color=\"#00c5d2\"))   # 添加银牌系列\n",
    "    .add_yaxis(\"铜牌\",[round(i[2],2) for i in dt3],stack='stack1',category_gap='55%',itemstyle_opts=itemstyle3,\n",
    "               label_opts=opts.LabelOpts(position=\"right\",formatter=\"{c}%\",color=\"#ffce2b\"))   # 添加铜牌系列\n",
    "    .set_series_opts(label_opts=opts.LabelOpts(position=\"right\",formatter=\"{c}%\")) # 设置系统标签的位置和格式\n",
    "    .set_global_opts(\n",
    "        title_opts=opts.TitleOpts(title='',pos_left='center',pos_top='2%'), # 设置图表标题\n",
    "        legend_opts=opts.LegendOpts(pos_top='7%'), # 设置图例位置\n",
    "        tooltip_opts=opts.TooltipOpts(\n",
    "            trigger='axis',\n",
    "            axis_pointer_type='shadow',\n",
    "            formatter=\"{b}<br/>{a0}: {c0}<br/{a2}>: {c2}%\"\n",
    "        ),\n",
    "        yaxis_opts=opts.AxisOpts(name='',axislabel_opts=opts.LabelOpts(rotate=20)), # 坐标轴标签旋转\n",
    "        xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(position='inside',formatter=\"{value}%\"),max_=100)\n",
    "    )\n",
    ")\n",
    "\n",
    "c.render(\"百分比堆叠柱形图.html\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4dc567f5-9a54-47f6-992a-19e97b521afc",
   "metadata": {},
   "source": [
    "可以生成但是图形不对"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "c6088e9a-b8a3-4149-a4b9-457f7576f904",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'D:\\\\jupyter\\\\历年奥运会中国奖牌数量百分比水平堆叠柱形图1.html'"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from pyecharts.charts import Bar # 绘制条形图的模块\n",
    "from pyecharts import options as opts  # 配置项模块\n",
    "from pyecharts.faker import Faker # 用于生成假数据\n",
    "from pyecharts.globals import ThemeType,JsCo # 主题配置项\n",
    "import pandas as pd\n",
    "data = pd.read_excel('./data/奖牌.xlsx')\n",
    "\n",
    "# 计算各种奖牌占总数的比例\n",
    "data['glod_p'] = data['金牌'] / data['总计']\n",
    "data['silver_p'] = data['银牌'] / data['总计']\n",
    "data['bronze_p'] = data['铜牌'] / data['总计']\n",
    "\n",
    "# 提取数据\n",
    "dt1 = data.loc[:,['赛事','金牌','glod_p']].values.tolist()\n",
    "dt2 = data.loc[:,['赛事','银牌','silver_p']].values.tolist()\n",
    "dt3 = data.loc[:,['赛事','铜牌','bronze_p']].values.tolist()\n",
    "\n",
    "# 定义基础样式\n",
    "base_itemstyle = {\n",
    "    \"normal\":{\n",
    "                \"shadowOffsetY\":1,\n",
    "                \"shadowOffsetX\":4,\n",
    "                \"barBorderRadius\":[2,2,2,2],\n",
    "                \"opacity\":1,\n",
    "                \"shadowBlur\":20\n",
    "                    }\n",
    "}\n",
    "\n",
    "# 创建不同奖牌的样式\n",
    "itemstyle1 = base_itemstyle.copy()  # 复制基础样式\n",
    "itemstyle1['normal']['shadowColor'] = 'rgba(78,112,240,.5)'  # 设置金牌的阴影颜色\n",
    "\n",
    "itemstyle2 = base_itemstyle.copy()  # 复制基础样式\n",
    "itemstyle2['normal']['shadowColor'] = 'rgba(0,197,210,.5)' # 设置银牌的阴影颜色\n",
    "\n",
    "itemstyle3 = base_itemstyle.copy()  # 复制基础样式\n",
    "itemstyle3['normal']['shadowColor'] = 'rgba(255,206,43,.5)' # 设置铜牌的阴影颜色\n",
    "\n",
    "# 数据标签格式化函数\n",
    "jscode = \"function(x){return Number(x.data * 100).toFixed(1) + '%';}\"\n",
    "\n",
    "# 坐标轴标签格式化函数\n",
    "jscode1 = \"function(x){return Number(x * 100).toFixed(1) + '%';}\"\n",
    "\n",
    "# 创建柱形图\n",
    "c = (\n",
    "    Bar(init_opts=opts.InitOpts(theme=ThemeType.LIGHT,bg_color='#ffffff')) # 初始化柱形图，设置主题和背景颜色\n",
    "    .add_xaxis([i[0] for i in dt1])  # 添加x轴数据，即赛事名称\n",
    "    .add_yaxis(\"金牌\",[round(i[2] ,2) for i in dt1],stack='stack1',category_gap='55%',itemstyle_opts=itemstyle1,\n",
    "               label_opts=opts.LabelOpts(position=\"right\",formatter=JsCode(jscode),color=\"#4e71f1\"))   # 添加金牌系列 \n",
    "    .add_yaxis(\"银牌\",[round(i[2],2) for i in dt2],stack='stack1',category_gap='55%',itemstyle_opts=itemstyle2,\n",
    "               label_opts=opts.LabelOpts(position=\"right\",formatter=JsCode(jscode),color=\"#00c5d2\"))   # 添加银牌系列\n",
    "    .add_yaxis(\"铜牌\",[round(i[2],2) for i in dt3],stack='stack1',category_gap='55%',itemstyle_opts=itemstyle3,\n",
    "               label_opts=opts.LabelOpts(position=\"right\",formatter=JsCode(jscode),color=\"#ffce2b\"))   # 添加铜牌系列\n",
    "    .reversal_axis()\n",
    "    .set_global_opts(\n",
    "        title_opts=opts.TitleOpts(title='历年奥运会中国奖牌数量百分比水平堆叠柱形图',pos_left='center',pos_top='2%'), # 设置图表标题及其位置\n",
    "        xaxis_opts=opts.AxisOpts(name='赛事名称',axislabel_opts=opts.LabelOpts(rotate=15)), # 设置x轴选项\n",
    "        toolbox_opts=opts.TooltipOpts(trigger='axis',axis_pointer_type='shadow'), # 设置提示框的触发方式和指针类型\n",
    "        # 设置y轴分割线的显示和样式\n",
    "        yaxis_opts=opts.AxisOpts(max_=1,axislabel_opts=opts.LabelOpts(formatter=JsCode(jscode1))) # 设置y轴选项\n",
    "    )\n",
    "    \n",
    ")\n",
    "c.render('历年奥运会中国奖牌数量百分比水平堆叠柱形图1.html')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3db2c06a-e157-429e-8301-bbe7f2283887",
   "metadata": {},
   "source": [
    "可以生成但是不对"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "560ab302-a5b7-441f-9f98-0c3b64b034e0",
   "metadata": {},
   "source": [
    "百分比堆叠柱形图不能直接添加'.reversal_axis()'把垂直变为水平，需要修改x，y轴。其他类型的可以。比如说，堆叠柱形图、多数据系列和单数据系列"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e86da323-f38d-42c4-9211-58d8e7da7e49",
   "metadata": {},
   "source": [
    "【11】"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2f922178-78b0-40a2-b0dc-6fd047491bf2",
   "metadata": {},
   "source": [
    "双向柱形图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "93669bf5-588f-4724-87ae-98d81bea6435",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'D:\\\\jupyter\\\\双向柱形图.html'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "from pyecharts import options as opts\n",
    "from pyecharts.charts import Bar\n",
    "from pyecharts.globals import ThemeType  # 导入主题\n",
    "from pyecharts.commons.utils import JsCode # 导入jscode\n",
    "data = pd.read_excel('./data/age.xlsx')\n",
    "\n",
    "# 将数据转换为列表\n",
    "data_pair = data.values.tolist()\n",
    "\n",
    "# 定义JavaScript代码\n",
    "jscode = \"function(x) { return Number(-x.data); }\"\n",
    "\n",
    "# 定义基础样式\n",
    "itemstyle1 = {\n",
    "    \"normal\":{\n",
    "                \"shadowOffsetY\":2,\n",
    "                \"color\":'#49a9dc',\n",
    "                \"shadowOffsetX\":2,\n",
    "                \"barBorderRadius\":[2,2,2,2],\n",
    "                \"opacity\":1,\n",
    "                \"shadowBlur\":2\n",
    "                    }\n",
    "}\n",
    "itemstyle2 = {\n",
    "    \"normal\":{\n",
    "                \"shadowOffsetY\":2,\n",
    "                \"color\":'#fded72',\n",
    "                \"shadowOffsetX\":2,\n",
    "                \"barBorderRadius\":[2,2,2,2],\n",
    "                \"opacity\":1,\n",
    "                \"shadowBlur\":2\n",
    "                    }\n",
    "}\n",
    "\n",
    "# 创建双向柱形图\n",
    "bar = (\n",
    "    Bar(init_opts=opts.InitOpts(theme=ThemeType.DARK))\n",
    "    .add_xaxis([i[0] for i in data_pair])\n",
    "    .add_yaxis(\n",
    "        '男性',\n",
    "        [i[1] for i in data_pair],\n",
    "        gap='-100%',\n",
    "        itemstyle_opts=itemstyle1,\n",
    "        label_opts=opts.LabelOpts(\n",
    "            position='top', # 柱形图的文本标签定位\n",
    "            font_weight=500\n",
    "        )  # 添加男性\n",
    "    )\n",
    "    .add_yaxis(\n",
    "        '女性',\n",
    "        [-i[1] for i in data_pair],  # 注意这里使用负值来表示女性数据\n",
    "        gap='-100%',\n",
    "        itemstyle_opts=itemstyle2,\n",
    "        label_opts=opts.LabelOpts(\n",
    "            position='bottom', # 柱形图的文本标签定位，标签位置在底部\n",
    "            font_weight=500,\n",
    "            formatter=JsCode(jscode) # 使用JavaScript代码格式化标签\n",
    "        )   # 添加女性\n",
    "    )\n",
    "    .set_global_opts(\n",
    "        tooltip_opts=opts.TooltipOpts(trigger='axis',axis_pointer_type='shadow'), # 设置提示框\n",
    "        yaxis_opts=opts.AxisOpts(is_show=False), # 隐藏y轴\n",
    "        xaxis_opts=opts.AxisOpts(\n",
    "            axistick_opts=opts.AxisTickOpts(is_show=False), # 隐藏x轴刻度曲线\n",
    "            axisline_opts=opts.AxisLineOpts(is_show=False), # 隐藏x轴线\n",
    "            axislabel_opts=opts.LabelOpts(font_weight=500,rotate=60) # 旋转x轴标签\n",
    "        )\n",
    "    )\n",
    ")\n",
    "bar.render('双向柱形图.html')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "31852088-c939-47f0-96b0-7b82ff74ad78",
   "metadata": {},
   "source": [
    "【11】/【0】"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "48f929c4-9f76-47da-8cb1-7e677957b46d",
   "metadata": {},
   "source": [
    "区域缩放柱形图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "ff5b54fc-6aa1-4208-938b-32276da92f04",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'D:\\\\jupyter\\\\render.html'"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from pyecharts.charts import Bar # 绘制条形图的模块\n",
    "from pyecharts import options as opts  # 配置项模块\n",
    "from pyecharts.faker import Faker # 用于生成假数据\n",
    "from pyecharts.globals import ThemeType # 主题配置项\n",
    "\n",
    "bar = (\n",
    "    Bar(init_opts=opts.InitOpts(theme=ThemeType.LIGHT))\n",
    "       .add_xaxis(Faker.days_attrs)\n",
    "       .add_yaxis(series_name=\"商家A\",\n",
    "                 y_axis=Faker.days_values,\n",
    "                 category_gap=\"60%\")\n",
    "       .set_global_opts(\n",
    "           datazoom_opts=[opts.DataZoomOpts(),\n",
    "                         opts.DataZoomOpts(type_=\"inside\")],# 添加两个数据缩放组件，一个为外部缩放，一个为内部缩放\n",
    "           title_opts=opts.TitleOpts(\n",
    "               title='区域缩放组件使用',\n",
    "               pos_left='center',  # 标题配置项的使用，标题内容标题位置\n",
    "               title_textstyle_opts=opts.TextStyleOpts(\n",
    "                   color='#fc97af',font_size=26), # 标题字体，字体颜色和大小\n",
    "               subtitle='这是副标题',\n",
    "               subtitle_textstyle_opts=opts.TextStyleOpts(\n",
    "                   color='cyan',font_size=14)),  # 副标题字体，字体颜色和大小\n",
    "           legend_opts=opts.LegendOpts(pos_top=\"12%\"),  # 图例的设置，位置的设置\n",
    "           xaxis_opts=opts.AxisOpts(\n",
    "               name='我是x轴',\n",
    "               name_textstyle_opts=opts.TextStyleOpts(color='cyan')\n",
    "           ),  # x轴坐标配置\n",
    "           yaxis_opts=opts.AxisOpts(\n",
    "               name='我是y轴',\n",
    "               name_textstyle_opts=opts.TextStyleOpts(color='cyan')\n",
    "           ))\n",
    "       .set_series_opts(\n",
    "        markline_opts=opts.MarkLineOpts(data=[   # 标记线的配置项\n",
    "            opts.MarkLineItem(type_='max',name='最大值'),\n",
    "            opts.MarkLineItem(type_='min',name='最小值'),\n",
    "            opts.MarkLineItem(type_='average',name='平均数')]),\n",
    "        markpoint_opts=opts.MarkPointOpts(data=[  # 标记点的配置项\n",
    "           opts.MarkPointItem(type_='max',name='最大值'),\n",
    "           opts.MarkPointItem(type_='min',name='最小值'),\n",
    "           opts.MarkPointItem(type_='average',name='平均数')]),\n",
    "        label_opts=opts.LabelOpts(is_show=False)) # 数据标签的配置\n",
    ")\n",
    "bar.render()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
